﻿using System;
using System.Runtime.Remoting.Messaging;

namespace Shumisen.Core.Composition.Aop
{
	public class Invocation : IInvocation
	{
		public Type TargetType { get; private set; }

		public Object TargetInstance { get; private set; }

		public IMethodCallMessage MethodCallMessage { get; private set; }

		public IMethodReturnMessage MethodReturnMessage { get; private set; }

		public Invocation(Type targetType, object targetInstance, IMethodCallMessage methodCallMessage)
		{
			TargetType = targetType;
			TargetInstance = targetInstance;
			MethodCallMessage = methodCallMessage;
		}

		public void Proceed()
		{
			var method = MethodCallMessage.MethodBase;
			var ret = method.Invoke(TargetInstance, MethodCallMessage.Args);
			MethodReturnMessage = new ReturnMessage(
				ret, MethodCallMessage.Args, MethodCallMessage.ArgCount, MethodCallMessage.LogicalCallContext, MethodCallMessage);
		}
	}
}
